package cn.com.twoke.game.client.games.gomoku.entity;

import cn.com.twoke.game.client.games.framework.component.LogicComponent;
import cn.com.twoke.game.client.games.framework.component.RenderComponent;
import cn.com.twoke.game.client.games.gomoku.constants.GomokuGameConstant;

import java.awt.*;
import java.awt.geom.Arc2D;

public class Chess implements RenderComponent, LogicComponent, GomokuGameConstant {

    private boolean display;
    private Point position;
    private Arc2D hitbox;
    private Type type;

    public Chess(int x, int y) {
        this.type = Type.NONE;
        this.display = false;
        this.position = new Point(x, y);
        this.hitbox = new Arc2D.Double(
                PANEL_PADDING_LEFT + CELL_WIDTH * x - CELL_WIDTH / 2.0,
                PANEL_PADDING_TOP + CELL_HEIGHT * y - CELL_HEIGHT / 2.0,
                CELL_WIDTH, CELL_HEIGHT, 0, 360,  Arc2D.PIE);
    }


    public Point getPosition() {
        return position;
    }

    public void setPosition(int x, int y) {
        this.position.x = x;
        this.position.y = y;
        this.hitbox.setArc(
                PANEL_PADDING_LEFT + CELL_WIDTH * x - CELL_WIDTH / 2.0,
                PANEL_PADDING_TOP + CELL_HEIGHT * y - CELL_HEIGHT / 2.0,
                CELL_WIDTH, CELL_HEIGHT, 0, 360,  Arc2D.PIE);
    }

    public void setDisplay(boolean display) {
        this.display = display;
    }

    public void setPosition(Point position) {
        this.position = position;
    }

    public Arc2D getHitbox() {
        return hitbox;
    }

    public void setHitbox(Arc2D hitbox) {
        this.hitbox = hitbox;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public enum Type {
        NONE(null, Chess.NONE, "-"),
        WHITE(Color.WHITE, Chess.WHITE, "白棋"),
        BLACK(Color.BLACK, Chess.BLACK, "黑棋");

        Type(Color color, int type, String  label) {
            this.color = color;
            this.type = type;
            this.label = label;
        }

        private final Color color;
        private final int type;
        private final String label;

        public int getType() {
            return type;
        }

        public String getLabel() {
            return label;
        }

        public Color getColor() {
            return color;
        }
    }

    public void drop(Type type) {
        this.type = type;
        this.show();
    }

    /**
     * 是否已经落子
     * @return
     */
    public boolean isDrop() {
        return this.type != Type.NONE;
    }

    @Override
    public void update(double deltaU) {

    }

    @Override
    public void render(Graphics2D g) {
        if (this.display && this.type != Type.NONE) {
            g.setColor(this.type.color);
            g.fill(hitbox);
        }
    }

    @Override
    public void show() {
        this.display = true;
    }

    @Override
    public void hide() {
        this.display = false;
    }

    public boolean isDisplay() {
        return display;
    }
}
